﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using App1AppHandler;
using Reborn_WorldServer;
using System.Drawing;

namespace Reborn_Server.App1AppHandler
{
    public class Spell_HealingLight : TargetAreaSpell
    {
        private int HealLow = 200;
        private int HealHigh = 240;

        public Spell_HealingLight() : base() { }


        public override bool hasCustomTarget(SpellIcon sIcon)
        {
            if (sIcon.Skill == "Holy")
                return true;

            return false;
        }

        private List<Point> hiddenList;
        private CreatureBase choosenTarget;

        public override bool hasValidTarget(SpellIcon sIcon, Creature creature, List<Point> hiddenList)
        {
            this.hiddenList = hiddenList;

            List<CreatureBase> targetCreatures = new List<CreatureBase>();
            foreach (MapItem mapitem in creature.currentMapItem.mapItemsWithin4)
            {
                foreach (CreatureBase creatureBase in mapitem.CreaturesBases)
                {
                    if (creatureBase.Faction == creature.Faction && creatureBase.HP < (creatureBase.MaxHP * .70))
                        targetCreatures.Add(creatureBase);
                }
            }

            int damagedAmount = 0;
            foreach (CreatureBase cb in targetCreatures)
            {
                if (CreatureAI.isHidden(cb.MapX, cb.MapY, hiddenList) == false)
                {
                    int damageValue = cb.MaxHP - cb.HP;
                    if (choosenTarget == null || damageValue > damagedAmount)
                    {
                        choosenTarget = cb;
                        damagedAmount = damageValue;
                    }
                }
            }

            if (choosenTarget == null)
                return false;
            else
                return true;
        }

        public void Cast(CreatureBase myCreatureBase, int CastLevel, long CreatureGUID)
        {
            //If its a creature custom cast, replace the targets ID
            if (choosenTarget != null)
                CreatureGUID = choosenTarget.CreatureID;

            List<MapItem> MapItemsWithin12;
            if (base.InitializeCast(myCreatureBase, CastLevel, CreatureGUID, true, out MapItemsWithin12) == false)
                return;


            MapItem targetsMapItem = null;
            List<Character> CharactersInUpdateRange = new List<Character>();
            foreach (MapItem mapitem in MapItemsWithin12)
            {
                if (mapitem.MapX == TargetCreatureBase.MapX && mapitem.MapY == TargetCreatureBase.MapY)
                    targetsMapItem = mapitem;

                foreach (CreatureBase creaturebase in mapitem.CreaturesBases)
                {
                    if (creaturebase is Character)
                        CharactersInUpdateRange.Add((Character)creaturebase);
                }
            }

            targetsMapItem.SpellLayer1.Add(this);
            MapItems.Add(targetsMapItem);

            foreach (CreatureBase creaturebase in targetsMapItem.CreaturesBases)
            {
                if (creaturebase.HP > 0 && creaturebase.Faction == TargetCreatureBase.Faction && creaturebase.HP < creaturebase.MaxHP)
                {
                    int healAmt = Managers.GameHandler.random.Next(HealLow, HealHigh + 1);
                    healAmt = SkillManager.AdjustForCombat(CastLevel, healAmt, CastingCreatureWis);

                    creaturebase.HP = creaturebase.HP + healAmt;
                    creaturebase.Update_Character();

                    if (myCreatureBase is Character)
                    {
                        Character character = (Character)myCreatureBase;

                        Message msg = new Message();
                        msg.CreatureID = character.CreatureID;
                        msg.Type = "spelldefense";
                        msg.Text = "You heal " + creaturebase.Name + " (" + healAmt.ToString() + ")";

                        character.AddMessage(msg);
                    }
                    if (creaturebase is Character && creaturebase != myCreatureBase)
                    {
                        Character character = (Character)creaturebase;

                        Message msg = new Message();
                        msg.CreatureID = character.CreatureID;
                        msg.Type = "spelldefense";
                        msg.Text = myCreatureBase.Name + " heals you (" + healAmt.ToString() + ")";

                        character.AddMessage(msg);
                    }
                }
            }

            foreach (Character character in CharactersInUpdateRange)
            {
                //Play Sound

                if (Math.Abs(character.MapX - TargetCreatureBase.MapX) <= 4 && Math.Abs(character.MapY - TargetCreatureBase.MapY) <= 4)
                    character.AddMapItem(targetsMapItem);
            }

        }
    }
}
